/**
 * 
 */
package com.xintoucloud.gsv.controller;


import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xintoucloud.gsv.entity.Dept;
import com.xintoucloud.gsv.entity.User;
import com.xintoucloud.gsv.model.DeptNode;
import com.xintoucloud.gsv.model.TreeData;
import com.xintoucloud.gsv.model.TreeNode;
import com.xintoucloud.gsv.service.DeptService;

/**
 * @author xiang
 *
 */
@Controller@RequestMapping("/dept")
public class DeptController {
	
	@Autowired
	private DeptService deptService;
	
	@RequestMapping("/showDeptTree")
	public String getDeptTree() {
		return "showdept";
	}
	
	@PostMapping("/list")
	@ResponseBody
	public TreeData showDeptTreeView(HttpSession session) {
		User user=(User) session.getAttribute("User");
		int rid=user.getRole().getRid();
		int oid=user.getDept().getDid();
		TreeData treeData=null;
		if(rid==1||rid==2) {
			treeData= deptService.getTreeData(0);
		}else if(rid==3) {
			treeData= deptService.getTreeData(oid);
		}else if(rid==4) {
			treeData= deptService.getTreeData(oid);
		}
		return treeData;
	}
	@PostMapping("/showDeptTree")
	@ResponseBody
	public List<DeptNode> showDeptTree() {
		 List<DeptNode> deptNodes= deptService.listDeptByParentId(0);
		 return deptNodes;
	}
	
	@PostMapping("/addDeptType")
	@ResponseBody
	public int addDeptType(int did,String dname) {
		Dept dept = deptService.saveDept(did, dname);
		return dept.getDid();
	}
	
	@PostMapping("/deleteDeptType")
	@ResponseBody
	public String deleteDeptType(int did) {
		deptService.deleteDeptById(did);
		return "redirect:showDeptTree";
	}
	
	@PostMapping("/updateDeptType")
	public String updateDeptType(int did,String dname) {
		deptService.updateDept(did, dname);
		return "redirect:showDeptTree";
	}
	
}
